package com.youmu.sichuansfc.service.impl;

import com.youmu.sichuansfc.dao.AreaDao;
import com.youmu.sichuansfc.domain.pojo.AreaPojo;
import com.youmu.sichuansfc.service.AreaService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WYZ
 * @create 2018-02-26 14:46
 * @desc
 **/
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    AreaDao areaDao;
    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public List<AreaPojo> findArea(Long parentId) {
        return areaDao.findByParantId(parentId);
    }

    @Override
    public List<AreaPojo> findAllAreaByParentId(Long parentId) {
        if(parentId == null)
            return  null;

        List<AreaPojo> areaPojoList = new ArrayList<>();
        AreaPojo parentArea = new AreaPojo();
        parentArea.setId(parentId);
        areaPojoList.add(parentArea);

        List<Long> parentIds = new ArrayList<Long>();
        parentIds.add(parentId);
        do{
            List<AreaPojo> areaPojos = areaDao.findByParantId(parentIds.toArray());
            parentIds.clear();
            if (areaPojos != null && areaPojos.size() > 0){
                for (AreaPojo areaPojo : areaPojos){
                    parentIds.add(areaPojo.getId());
                    areaPojoList.add(areaPojo);
                }
                logger.info(parentIds.toString());
            }else {
                return areaPojoList;
            }
        }while(true);
    }
}
